package com.cql.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cql.common.constant.FoodCircleConstant;
import com.cql.common.constant.HttpStatus;
import com.cql.common.core.DO.MenuDO;
import com.cql.common.core.DO.MenuStepDO;
import com.cql.common.core.DTO.PictureDTO;
import com.cql.common.core.DTO.StepDTO;
import com.cql.common.exception.CustomException;
import com.cql.common.utils.StringUtils;
import com.cql.mapper.MenuMapper;
import com.cql.mapper.MenuStepMapper;
import com.cql.service.business.MenuService;
import com.cql.service.business.MenuStepService;
import com.cql.service.business.PictureService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cql
 */
@Service
public class MenuStepServiceImpl extends ServiceImpl<MenuStepMapper, MenuStepDO> implements MenuStepService {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MenuStepMapper menuStepMapper;

    @Autowired
    private PictureService pictureService;

    @Override
    public void insertSteps(List<StepDTO> steps, Long menuId) {
        if (StringUtils.isNotEmpty(steps)) {
            if (StringUtils.isNull(menuId)) {
                throw new CustomException(HttpStatus.InnerError.getMessage());
            }
            List<MenuStepDO> list = new ArrayList<>();
            for (StepDTO step : steps) {
                MenuStepDO stepDO = new MenuStepDO();
                stepDO.setMenuId(menuId);
                stepDO.setStepIndex(step.getIndex());
                stepDO.setDescription(step.getDesc());
                if (StringUtils.isNotNull(step.getPicture())) {
                    stepDO.setImg(step.getPicture().getSrc());
                }
                list.add(stepDO);
            }
            // 批量增加
            saveBatch(list);
        }
    }

    @Override
    public void deleteMenuSteps(Long menuId) {
        if (StringUtils.isNotNull(menuId)) {

            /**
             * 查询出该菜单所有步骤
             */
            QueryWrapper<MenuStepDO> wrapper = new QueryWrapper<>();
            wrapper.eq("menu_id", menuId);

            // 移除该菜谱所有步骤
            menuStepMapper.delete(wrapper);

            List<String> imgs = menuStepMapper.selectList(wrapper).stream().map(s -> {
                return s.getImg();
            }).collect(Collectors.toList());

            pictureService.deleteImgsBySrc(imgs);
        }
    }

    @Override
    public List<StepDTO> listMenuStep(Long menuId) {
        List<StepDTO> result = new ArrayList<>();

        if (StringUtils.isNotNull(menuId)) {
            QueryWrapper<MenuStepDO> wrapper = new QueryWrapper<>();
            wrapper.eq("menu_id", menuId);
            List<MenuStepDO> menuStepDOS = menuStepMapper.selectList(wrapper);

            for (MenuStepDO stepDO : menuStepDOS) {
                StepDTO s = new StepDTO();
                s.setIndex(stepDO.getStepIndex());
                s.setDesc(stepDO.getDescription());
                PictureDTO p = pictureService.getPictureBySrc(stepDO.getImg());
                s.setPicture(p);
                result.add(s);
            }

        }

        return result;
    }
}
